﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace UI.Forms
{
    public partial class whd_FK_Input : FormBase1
    {
        BindingList<SVC.client.View_whd_d_1> vd1s;
        BindingList<SVC.client.View_Emp_lite> emps;
        public whd_FK_Input()
        {
            InitializeComponent();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            DateTime? sd = null;
            DateTime? ed = null;
            Guid? infoempid = null;
            int? did=null;
            if (dateselect1.de_sd.EditValue != null && dateselect1.de_ed.EditValue != null)
            {
                sd = dateselect1.de_sd.DateTime;
                ed = dateselect1.de_ed.DateTime;
            }
            try 
	        {	        
		        did=int.Parse(te_did.Text);
	        }
	        catch{}
	        
            vd1s.Clear();
            foreach (SVC.client.View_whd_d_1 item in my.bw.View_whd_d_1_get_pid("", sd, ed, 2, did, my.ACID))
            {
                vd1s.Add(item);
            }
        }

        private void whd_FK_Input_Load(object sender, EventArgs e)
        {
            dateselect1.lue_date.Properties.DataSource = my.dts;
            dateselect1.lue_date.EditValue = 5;
            vd1s = new BindingList<SVC.client.View_whd_d_1>();
            emps =my.bw.View_Emp_lite_List("",my.ACID);
            lue_ldr.Properties.DataSource = emps;
            lue_sdr.Properties.DataSource = emps;
            lue_whr.Properties.DataSource = emps;
            lue_sdr.EditValue = my.emp_lite.EmplID;
            gridControl1.DataSource = vd1s;
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.Clicks == 2)
            {
                if (e.RowHandle >= 0)
                {
                    SVC.client.View_whd_d_1 d1 = gridView1.GetRow(e.RowHandle) as SVC.client.View_whd_d_1;
                    SVC.client.whd_d_fk fk = my.bw.whd_d_fkBydid_Get(d1.did, my.ACID);
                    te_did1.Text = fk.did.ToString();
                    te_clientname.Text = d1.用户名称;
                    lue_whr.EditValue = fk.whr;
                    lue_sdr.EditValue = my.emp_lite.EmplID; ;

                }
            }
        }

        private void btn_savenext_Click(object sender, EventArgs e)
        {
            if (!dvp.Validate()) return;
            SVC.client.whd_d_fk fk = my.bw.whd_d_fkBydid_Get(int.Parse(te_did1.Text),my.ACID);
            string r = string.Empty;
            fk.sdr = new Guid(lue_sdr.EditValue.ToString());
            fk.whr = new Guid(lue_whr.EditValue.ToString());
            fk.ldr = new Guid(lue_ldr.EditValue.ToString());
            fk.jrh = te_jrh.Text;
            fk.linktel = te_linktel.Text;
            fk.ytc = te_ytc.Text;
            fk.xtc = te_xtc.Text;
            fk.yysj = de_yysj.DateTime;
            fk.skje = Convert.ToDecimal(sp_skje.EditValue);
            fk.bzzp = me_bzzp.Text;
            fk.fkjg = me_fkjg.Text;
            fk.fksm = me_fksm.Text;
            fk.ssje=Convert.ToDecimal(sp_ssje.EditValue);
            fk.sdbz = me_sdbz.Text;
            fk.fspff = me_fspff.Text;
            r = my.bw.ld(fk,4, my.ACID);
            if (!string.IsNullOrEmpty(r))
            {
                Comm.ShowErorr(r);
            }
            else
            {
                Comm.ShowInfo("数据提交成功！");
                vd1s.Remove(vd1s.Single(p => p.did == fk.did));
                ResetEditor();
            }
        }

        /// <summary>
        /// 清除编辑界面数据
        /// </summary>
        private void ResetEditor()
        {
            te_did1.Text = "";
            te_clientname.Text = "";
            lue_ldr.EditValue = null;
            lue_whr.EditValue = null;
            te_jrh.Text = "";
            te_linktel.Text = "";
            te_ytc.Text = "";
            te_xtc.Text = "";
            de_yysj.EditValue = null;
            sp_skje.EditValue = 0;
            me_bzzp.Text = "";
            me_fkjg.Text = "";
            me_fksm.Text = "";
        }

        private void btn_stop_Click(object sender, EventArgs e)
        {
            if (!dvp1.Validate()) return;
            SVC.client.whd_d_fk fk = my.bw.whd_d_fkBydid_Get(int.Parse(te_did1.Text), my.ACID);
            string r = string.Empty;
            fk.fkjg = me_fkjg.Text;
            fk.fksm = me_fksm.Text;
            r = my.bw.ld(fk, -1, my.ACID);
            if (!string.IsNullOrEmpty(r))
            {
                Comm.ShowErorr(r);
            }
            else
            {
                Comm.ShowInfo("数据提交成功！");
                vd1s.Remove(vd1s.Single(p => p.did == fk.did));
                ResetEditor();
            }
        }
    }
}